package com.xiaoma.tpo.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import junit.runner.Version;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.xiaoma.tpo.dao.AdAdDao;
import com.xiaoma.tpo.dao.AdPositionDao;
import com.xiaoma.tpo.dao.AdVisitDao;
import com.xiaoma.tpo.dao.VPhysicalAddressDao;
import com.xiaoma.tpo.dao.VVersionDao;
import com.xiaoma.tpo.domain.AdAd;
import com.xiaoma.tpo.domain.AdPosition;
import com.xiaoma.tpo.domain.AdVisit;
import com.xiaoma.tpo.domain.VPhysicalAddress;
import com.xiaoma.tpo.domain.VVersion;
import com.xiaoma.tpo.dto.AdVo;
import com.xiaoma.tpo.dto.PhyVersionVO;
import com.xiaoma.tpo.dto.SelectBoxVO;
import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.tpo.utils.UploadImgUtil;

@Service
@Transactional
public class AdService {
	@Autowired
	private AdAdDao adAdDao;
	
	@Autowired
	private AdPositionDao adPositionDao;
	
	@Autowired
	private VVersionDao vVersionDao;
	
	@Autowired
	private VPhysicalAddressDao vPhysicalAddressDao;
	
	@Autowired
	private AdVisitDao adVisitDao;
	public String saveAd(){
		return null;
	}

	public Pager listAd(String day,String sStatus,String sPosition,String sName, Integer page, Integer rows) {
		Pager pager = adAdDao.listAd(day,sStatus,sPosition,sName,page,rows);
		return pager;
	}
	//添加广告
	public boolean addAd(MultipartFile file,AdVo adVo) {
		String url = uploadImage(file);
		//在保存广告加上位置ID
		AdAd adAd = new AdAd();
		adAd.setPositionId(adVo.getPositionId());
		adAd.setStatus(adVo.getStatus());
		adAd.setImageUrl(url);
		adAd.setContent(adVo.getContent());
		adAd.setDescription(adVo.getDescription());
		adAd.setCreateDate(new Date());
		adAd.setUpdateDate(new Date());
		int rAdAdId = adAdDao.insert(adAd);
		if(rAdAdId < 0 ){
			return false;
		}
		return true;
	}
	
	//ckeditor上传图片
	public String uploadImage(MultipartFile image){
		//将图片保存到右拍云
				String path = null;
				try {
					if(image != null && !image.isEmpty()){
						Integer lastIndex = image.getOriginalFilename().lastIndexOf(".");
						String postfix = image.getOriginalFilename().substring(lastIndex);
						String fileName = UUID.randomUUID().toString()+postfix;
						 path = UploadImgUtil.uploadImg(image.getBytes(), fileName);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				return path;
	}

	public boolean editAd(MultipartFile file,AdVo adVo) {
		String url = uploadImage(file);
			//在更新广告加上位置ID
			AdAd adAd  = adAdDao.getById(adVo.getId());
			adAd.setPositionId(adVo.getPositionId());
			adAd.setStatus(adVo.getStatus());
			adAd.setImageUrl(url);
			adAd.setContent(adVo.getContent());
			adAd.setDescription(adVo.getDescription());
			adAd.setUpdateDate(new Date());
			int rAdAdId = adAdDao.update(adAd);
			if( rAdAdId < 0 ){
				return false;
			}
			return true;
	}

	public boolean removeAd(Integer id) {
		AdAd rAdAd = adAdDao.getById(id);
		int aaId=0;
		if(rAdAd != null ){
			aaId =adAdDao.destroy(rAdAd.getId());
		}
		if(aaId <= 0 ){
			return false;
		}
		return true;
	}
	/**
	 *
	 *  位置
	 * 
	 */
	public List<AdPosition> getAllPosition() {
		List<AdPosition> aPositions = adPositionDao.findAll();
		Collections.sort(aPositions, new AdPosition());
		return aPositions;
	}
	//添加位置
	public boolean addPosition(AdPosition adPosition) {
		
		//在保存广告加上位置ID
				AdPosition nNdPosition = new AdPosition();
				nNdPosition.setId(adPosition.getId());
				nNdPosition.setName(adPosition.getName());
				int rAdPositionId = adPositionDao.insert(adPosition);
				if(rAdPositionId < 0 ){
					return false;
				}
				return true;
	}
	//修改位置
	public boolean editAdPosition(AdVo adVo) {
		
		//在更新广告加上位置ID
		AdPosition adPosition  = adPositionDao.getById(adVo.getId());
		adPosition.setPosition(adVo.getPosition());
		adPosition.setName(adVo.getName());
		int rAdAdPositionId = adPositionDao.update(adPosition);
		if( rAdAdPositionId < 0 ){
			return false;
		}
		return true;
	}
	public boolean removeAdPosition(Integer id) {
		AdPosition rAdPosition = adPositionDao.getById(id);
		int adPositionId=0;
		if(rAdPosition != null ){
			adPositionId =adPositionDao.destroy(rAdPosition.getId());
		}
		if(adPositionId <= 0 ){
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 *  版本的物理地址 
	 */
	public List<VPhysicalAddress> getAllPAddress() {
		List<VPhysicalAddress> vPhysicalAddresss = vPhysicalAddressDao.findAll();
		return vPhysicalAddresss;
	}
	//添加位置
	public boolean addPAddress(VPhysicalAddress vPhysicalAddress) {
		
		//在保存广告加上位置ID
		VPhysicalAddress nVPhysicalAddress = new VPhysicalAddress();
		nVPhysicalAddress.setId(vPhysicalAddress.getId());
		nVPhysicalAddress.setName(vPhysicalAddress.getName());
		nVPhysicalAddress.setGuid(vPhysicalAddress.getGuid());
				int rVPhysicalAddressId = vPhysicalAddressDao.insert(nVPhysicalAddress);
				if(rVPhysicalAddressId < 0 ){
					return false;
				}
				return true;
	}
	//修改位置
	public boolean editPAddress(VPhysicalAddress vPhysicalAddress) {
		
		//在更新广告加上位置ID
		VPhysicalAddress rVPhysicalAddress  = vPhysicalAddressDao.getById(vPhysicalAddress.getId());
		rVPhysicalAddress.setGuid(vPhysicalAddress.getGuid());
		rVPhysicalAddress.setName(vPhysicalAddress.getName());
		int rVPhysicalAddressId = vPhysicalAddressDao.update(rVPhysicalAddress);
		if( rVPhysicalAddressId < 0 ){
			return false;
		}
		return true;
	}
	public boolean removePAddress(Integer id) {
		VPhysicalAddress rVPhysicalAddress = vPhysicalAddressDao.getById(id);
		int adVPhysicalAddressId=0;
		if(rVPhysicalAddress != null ){
			adVPhysicalAddressId =vPhysicalAddressDao.destroy(rVPhysicalAddress.getId());
		}
		if(adVPhysicalAddressId <= 0 ){
			return false;
		}
		return true;
	}
	/**
	 * 
	 *版本控制 
	 * 
	 */
	public Pager listVersion(String day, Integer page, Integer rows) {
		Pager pager = vVersionDao.listVersion(day,page,rows);
		return pager;
	}
	//添加广告
	public boolean addVersion(VVersion vVersion) {
		VVersion rVVersion = new VVersion();
		if(vVersionDao.isMostVersion(vVersion)){
			List<VVersion> vVersions = vVersionDao.getVersionByIsLatestAndGuid(vVersion);
			if(vVersions != null && vVersions.size() > 0){
				VVersion sVVersion = vVersions.get(0);
				sVVersion.setIsLatest("0");
				vVersionDao.update(sVVersion);
				rVVersion.setIsLatest("1");
			}
		}else{
			rVVersion.setIsLatest("0");
		}
		//在保存广告加上位置ID
		rVVersion.setVersion(vVersion.getVersion());
		rVVersion.setDescription(vVersion.getDescription());
		rVVersion.setDownloadUrl(vVersion.getDownloadUrl());
		rVVersion.setPlatformGuid(vVersion.getPlatformGuid());
		rVVersion.setIsForceUpdate(vVersion.getIsForceUpdate());
		rVVersion.setCreateDate(new Date());
		rVVersion.setUpdateDate(new Date());
		int rVVersionId = vVersionDao.insert(rVVersion);
		if(rVVersionId < 0 ){
			return false;
		}
		return true;
	}

	public boolean editVersion(VVersion vVersion) {
		
			//在更新广告加上位置ID
			VVersion rVVersion  = vVersionDao.getById(vVersion.getId());
			rVVersion.setVersion(vVersion.getVersion());
			rVVersion.setDescription(vVersion.getDescription());
			rVVersion.setIsLatest(vVersion.getIsLatest());
			rVVersion.setDownloadUrl(vVersion.getDownloadUrl());
			rVVersion.setPlatformGuid(vVersion.getPlatformGuid());
			rVVersion.setIsForceUpdate(vVersion.getIsForceUpdate());
			rVVersion.setUpdateDate(new Date());
			int rVVersionId = vVersionDao.update(rVVersion);
			if( rVVersionId < 0 ){
				return false;
			}
			return true;
	}

	public boolean removeVersion(Integer id) {
		VVersion rVVersion = vVersionDao.getById(id);
		if("1".equals(rVVersion.getIsLatest())){
			VVersion lVVersion = vVersionDao.getLastLatestVersion(rVVersion);
			vVersionDao.update(lVVersion);
		}
		int rVVersionId=0;
		if(rVVersion != null ){
			rVVersionId =vVersionDao.destroy(rVVersion.getId());
		}
		if(rVVersionId <= 0 ){
			return false;
		}
		return true;
	}

	public List<AdVo> getAllAd() {
		List<AdVo> adAds = adAdDao.getAllAd();
		return adAds;
	}

	public List<PhyVersionVO> getNewVersion(String guid,String version) {
		List<PhyVersionVO> vVersions = vVersionDao.getNewVersion(guid,version);
		return vVersions;
	}

	public List<SelectBoxVO> getComboAd() {
		List<AdPosition> adPositions = adPositionDao.findAll();
		List<SelectBoxVO> sbvo = new ArrayList<SelectBoxVO>();
		SelectBoxVO sbv0 = new SelectBoxVO();
		sbv0.setTextField("全部");
		sbv0.setValueField("");
		sbvo.add(sbv0);
		for(AdPosition adPosition :adPositions){
			SelectBoxVO sbv = new SelectBoxVO();
			sbv.setTextField(adPosition.getName());
			sbv.setValueField(adPosition.getId().toString());
			sbvo.add(sbv);
		}
		return sbvo;
		
	}

	public List<SelectBoxVO> getComboPlatform() {
		List<VPhysicalAddress> vPhysicalAddresses = vPhysicalAddressDao.findAll();
		List<SelectBoxVO> sbvo = new ArrayList<SelectBoxVO>();
		for(VPhysicalAddress vPhysicalAddress :vPhysicalAddresses){
			SelectBoxVO sbv = new SelectBoxVO();
			sbv.setTextField(vPhysicalAddress.getName());
			sbv.setValueField(vPhysicalAddress.getGuid().toString());
			sbvo.add(sbv);
		}
		return sbvo;
	}

	public boolean judgeActiveAd(AdVo adVo) {
		boolean b = adAdDao.judgeActiveAd(adVo);
		return b;
	}

	public Pager adCount(Integer adId,Integer positionId,String platformGuid,Integer page,Integer rows) {
		Pager pager = adAdDao.adCount(adId,positionId,platformGuid,page,rows);
		return pager;
	}
	//获取平台下的查询下拉菜单
	public List<SelectBoxVO> getComboClass() {
		List<VPhysicalAddress> vPhysicalAddresses = vPhysicalAddressDao.findAll();
		List<SelectBoxVO> sbvo = new ArrayList<SelectBoxVO>();
		SelectBoxVO sbv0 = new SelectBoxVO();
		sbv0.setValueField("");
		sbv0.setTextField("全部");
		sbvo.add(sbv0);
		for(VPhysicalAddress vPhysicalAddress :vPhysicalAddresses){
			SelectBoxVO sbv = new SelectBoxVO();
			sbv.setTextField(vPhysicalAddress.getName());
			sbv.setValueField(vPhysicalAddress.getGuid().toString());
			sbvo.add(sbv);
		}
		return sbvo;
	}

	public Integer addAdCount(AdVisit adVisit) {
		synchronized(this){
			AdVisit adCount = new AdVisit(); 
			adCount.setAdId(adVisit.getAdId());
			adCount.setPositionId(adVisit.getPositionId());
			List<Object> lAdCounts = adVisitDao.querySql(adCount);
			if(lAdCounts != null && lAdCounts.size() > 0){
				//更新
				AdVisit rAdCount = (AdVisit) lAdCounts.get(0);
				rAdCount.setCount(rAdCount.getCount()+1);
				int b =adVisitDao.update(rAdCount);
				return b;
			}
			//否则插入
			AdVisit adCount1 = new AdVisit();
			adCount1.setAdId(adVisit.getAdId());
			adCount1.setPositionId(adVisit.getPositionId());
			adCount1.setPlatformGuid(adVisit.getPlatformGuid());
			adCount1.setCount(1);
			int b = adVisitDao.insert(adCount1);
			return b;
		}
	}

	public List<PhyVersionVO> getVersion(String guid) {
		List<PhyVersionVO> vVersions = vVersionDao.getNewVersion(guid);
		return vVersions;
	}
	//强制更新为最新版本
	public Integer forceUpdate(Integer id,String guid) {
		VVersion  vVersion = new VVersion();
		vVersion.setPlatformGuid(guid);
		List<VVersion> vVersions = vVersionDao.getVersionByIsLatestAndGuid(vVersion);
		if(vVersions != null && vVersions.size() > 0){
			VVersion sVVersion = vVersions.get(0);
			sVVersion.setIsLatest("0");
			vVersionDao.update(sVVersion);
		}
		VVersion vVersion2 = vVersionDao.getById(id);
		vVersion2.setId(id);
		vVersion2.setIsLatest("1");
		vVersionDao.update(vVersion2);
		return 1;
	}
	//避免重复的验证
	public boolean validate(VVersion vVersion) {
		VVersion vVersion2 = new VVersion();
		vVersion2.setVersion(vVersion.getVersion());
		List<VVersion> vVersions = vVersionDao.findByColumns(vVersion2);
		if(vVersions != null && vVersions.size()>0){
			Integer id = vVersions.get(0).getId();
			//这个if是处理编辑时候的重复
			if(vVersion.getId() != null){
				if(vVersion.getId().intValue() == id.intValue()){
					return false;
				}
			}
			return true;
		}
		return false;
		
	}
	
}
